'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const P = require('../nodes/Collection.cjs'),
  y = require('../nodes/identity.cjs'),
  v = require('./stringify.cjs'),
  S = require('./stringifyComment.cjs');
function b(c, r, o) {
  return (r.inFlow ?? c.flow ? F : j)(c, r, o);
}
function j(
  { comment: c, items: r },
  o,
  {
    blockItemPrefix: u,
    flowChars: m,
    itemIndent: p,
    onChompKeep: h,
    onComment: C,
  },
) {
  const {
      indent: $,
      options: { commentString: B },
    } = o,
    q = Object.assign({}, o, { indent: p, type: null });
  let e = !1;
  const s = [];
  for (let t = 0; t < r.length; ++t) {
    const l = r[t];
    let g = null;
    if (y.isNode(l))
      !e && l.spaceBefore && s.push(''),
        k(o, s, l.commentBefore, e),
        l.comment && (g = l.comment);
    else if (y.isPair(l)) {
      const n = y.isNode(l.key) ? l.key : null;
      n && (!e && n.spaceBefore && s.push(''), k(o, s, n.commentBefore, e));
    }
    e = !1;
    let f = v.stringify(
      l,
      q,
      () => (g = null),
      () => (e = !0),
    );
    g && (f += S.lineComment(f, p, B(g))), e && g && (e = !1), s.push(u + f);
  }
  let i;
  if (s.length === 0) i = m.start + m.end;
  else {
    i = s[0];
    for (let t = 1; t < s.length; ++t) {
      const l = s[t];
      i += l
        ? `
${$}${l}`
        : `
`;
    }
  }
  return (
    c
      ? ((i +=
          `
` + S.indentComment(B(c), $)),
        C && C())
      : e && h && h(),
    i
  );
}
function F(
  { comment: c, items: r },
  o,
  { flowChars: u, itemIndent: m, onComment: p },
) {
  const {
    indent: h,
    indentStep: C,
    flowCollectionPadding: $,
    options: { commentString: B },
  } = o;
  m += C;
  const q = Object.assign({}, o, { indent: m, inFlow: !0, type: null });
  let e = !1,
    s = 0;
  const i = [];
  for (let f = 0; f < r.length; ++f) {
    const n = r[f];
    let a = null;
    if (y.isNode(n))
      n.spaceBefore && i.push(''),
        k(o, i, n.commentBefore, !1),
        n.comment && (a = n.comment);
    else if (y.isPair(n)) {
      const d = y.isNode(n.key) ? n.key : null;
      d &&
        (d.spaceBefore && i.push(''),
        k(o, i, d.commentBefore, !1),
        d.comment && (e = !0));
      const N = y.isNode(n.value) ? n.value : null;
      N
        ? (N.comment && (a = N.comment), N.commentBefore && (e = !0))
        : n.value == null && d != null && d.comment && (a = d.comment);
    }
    a && (e = !0);
    let w = v.stringify(n, q, () => (a = null));
    f < r.length - 1 && (w += ','),
      a && (w += S.lineComment(w, m, B(a))),
      !e &&
        (i.length > s ||
          w.includes(`
`)) &&
        (e = !0),
      i.push(w),
      (s = i.length);
  }
  let t;
  const { start: l, end: g } = u;
  if (i.length === 0) t = l + g;
  else if (
    (e ||
      (e =
        i.reduce((n, a) => n + a.length + 2, 2) >
        P.Collection.maxFlowStringSingleLineLength),
    e)
  ) {
    t = l;
    for (const f of i)
      t += f
        ? `
${C}${h}${f}`
        : `
`;
    t += `
${h}${g}`;
  } else t = `${l}${$}${i.join(' ')}${$}${g}`;
  return c && ((t += S.lineComment(t, h, B(c))), p && p()), t;
}
function k({ indent: c, options: { commentString: r } }, o, u, m) {
  if ((u && m && (u = u.replace(/^\n+/, '')), u)) {
    const p = S.indentComment(r(u), c);
    o.push(p.trimStart());
  }
}
exports.stringifyCollection = b;
